﻿function deleteobj(t) {
    if (!confirm("您真的要删除项吗？"))
        return;
    var selt = $(t);
    var td = selt.parents("td");
    var tds = td.siblings("td[field=roleid]");
    var id = tds.children("div").text();
    $.ajax({
    url: '../Admin/DelteRole',
        type: 'Post',
        data: { id: id },
        timeout: 20000,
        error: function(data) {
            alert("发生未知异常，删除失败，请您稍候再试！");
        },
        success: function(data) {
            if (data == 1) {
                $('#test').datagrid('reload');

            } else {
            alert(data);
            }
        }
    });
}
//修改权限
function changeRole(t) {
    var selt = $(t);
    var td = selt.parents("td");
    var tds = td.siblings("td[field=roleid]");
    var id = tds.children("div").text();
    window.self.parent.addTab('修改权限', '../Admin/ChangeAuthor/' + id, 'icon-users');

}
///加载数据到datagrid
function CreateDataGrid() {

    var result = $("#result");
    var w = result.parent("div").width() * 0.97;
    var lwidth = w * 0.6;
    var rwidth = w * 0.3;
    var test = $('#test');
    test.datagrid({
        title: '系统角色信息',
        iconCls: 'icon-save',
        width: w,
        height: 350,
        nowrap: false,
        striped: true,
        collapsible: true,
        url: '../Admin/GetRoles',
        loadMsg: "正在加载数据......",
        columns: [[
			        { title: '角色信息', colspan: 2 },
					{ field: 'opt', title: '操作', width: rwidth, align: 'center', rowspan: 1,
					    formatter: function(value, rec) {
					    return '<span style=" cursor: pointer;"><a href="javascript:void(0)"  onclick="changeRole(this)">修改权限</a><a  href="javascript:void(0)"  onclick="deleteobj(this)"> 删除</a></span>';
					    }
					}
				], [
					{ field: 'roleid', title: '', width: 0 },
					{ field: 'rolename', title: '角色名称', width: lwidth, rowspan: 1 }

				]],
        pagination: false,
        rownumbers: false
    });
    test.datagrid('hideColumn', 'roleid');
}

$(function() {
    CreateDataGrid();
    var userForm = $("#userForm");
    var tbs = userForm.children("table").children("tbody").children("tr").children("td");
    userForm.validate({
        rules: {
            roleName: {
                required: true,
                minlength: 2,
                remote: {
                    url: "../Admin/VerifyRoleName",     //后台处理程序 
                    type: "post",               //数据发送方式 
                    dataType: "json"           //接受数据格式    
                }
            }
        }
    });
    //ajaxform 添加信息
    function ValidRequest() {
        return userForm.validate().form(); // 返回是否验证成功            
    }
    function showResponse(responseText, statusText) {
        if (responseText == "1") {
            userForm.resetForm();
            CreateDataGrid();
        } else {
            alert(responseText);
        }
    }
    var options = {
        url: '../Admin/AddRolePost',
        beforeSubmit: ValidRequest,
        success: showResponse
    }
    userForm.ajaxForm(options);
});